package com.denlaku.longan.event;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author tianx
 */
@Configuration
public class EventConfig {
    @Bean
    public MessageEventProducer messageEventProducer(List<MessageEventHandler> handlers) {
        Disruptor<MessageEvent> disruptor = new Disruptor<>(new MessageEventFactory(), 128,
            new MessageThreadFactory(), ProducerType.SINGLE, new BlockingWaitStrategy());
        disruptor.handleEventsWith(new MessageEventHandlers(handlers));
        disruptor.setDefaultExceptionHandler(new MessageExceptionHandler());
        RingBuffer<MessageEvent> ringBuffer = disruptor.start();
        return new MessageEventProducer(ringBuffer);
    }
}
